<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       div{
        height: 150px;
        width: 150px;
        background-color: pink;
        position: absolute;
        left: 0;
        top: 0;
        border-radius: 50%;
        }
    </style>
</head>
<body>
    <div></div>
    <script>
        var div = document.querySelector('div')
        //获取到页面元素
        document.onkeydown = function (e) {
            //为什么要用onkeydown 是因为只有onkeydown是按下打印一次
            e = e || window.event
            //触发了event 事件
            var code = e.keyCode || e.which
            console.log(e.keyCode)
            console.log(code)
            switch (code) {
                case 38:
                    div.style.top =div.offsetTop - 10 + 'px'
                    break
                case 40:
                    div.style.top = div.offsetTop + 10 + 'px'
                    break
                case 37:
                    div.style.left =div.offsetLeft - 10 + 'px'
                    break
                case 39:
                    div.style.left = div.offsetLeft + 10 + 'px'
                    break
            }
        }
    </script>
</body>
</html>